EC-CUBE 2.4.4
[ class tree: EC-CUBE 2.4.4 ] [ index: EC-CUBE 2.4.4 ] [ all elements ]

Source for file SC_Query.php

Documentation is available at SC_Query.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2010 LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.co.jp/
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  */
  23.  
  24. /**
  25.  * SQLの構築・実行を行う
  26.  *
  27.  * @author LOCKON CO.,LTD.
  28.  * @version $Id: SC_Query.php 18735 2010-06-22 08:53:31Z nanasess $
  29.  */
  30. class SC_Query {
  31.     var $option;
  32.     var $where;
  33.     var $conn;
  34.     var $groupby;
  35.     var $order;
  36.  
  37.     /**
  38.      * コンストラクタ.
  39.      *
  40.      * @param $dsn 
  41.      * @param boolean $err_disp エラー表示を行うかどうか
  42.      * @param boolean $new 新規に接続を行うかどうか
  43.      * @return SC_Query 
  44.      */
  45.     function SC_Query($dsn ""$err_disp true$new false{
  46.         $this->conn = new SC_DBconn($dsn$err_disp$new);
  47.         $this->where = "";
  48.     }
  49.  
  50.     /**
  51.      *  エラー判定を行う.
  52.      *
  53.      * @return boolean 
  54.      */
  55.     function isError({
  56.         if(PEAR::isError($this->conn->conn)) {
  57.             return true;
  58.         }
  59.         return false;
  60.     }
  61.  
  62.     /**
  63.      * COUNT文を実行する.
  64.      *
  65.      * @param string $table テーブル名
  66.      * @param string $where where句
  67.      * @param array $arrval プレースホルダ
  68.      * @return integer 件数
  69.      */
  70.     function count($table$where ""$arrval array()) {
  71.         if(strlen($where<= 0{
  72.             $sqlse "SELECT COUNT(*) FROM $table";
  73.         else {
  74.             $sqlse "SELECT COUNT(*) FROM $table WHERE $where";
  75.         }
  76.         // カウント文の実行
  77.         $ret $this->conn->getOne($sqlse$arrval);
  78.         return $ret;
  79.     }
  80.  
  81.     /**
  82.      * SELECT文を実行する.
  83.      *
  84.      * @param string $col カラム名. 複数カラムの場合はカンマ区切りで書く
  85.      * @param string $table テーブル名
  86.      * @param string $where WHERE句
  87.      * @param array $arrval プレースホルダ
  88.      * @return array|null
  89.      */
  90.     function select($col$table$where ""$arrval array()){
  91.         $sqlse $this->getSql($col$table$where);
  92.         // DBに依存した SQL へ変換
  93.         $dbFactory SC_DB_DBFactory_Ex::getInstance();
  94.         $sqlse $dbFactory->sfChangeMySQL($sqlse);
  95.         $ret $this->conn->getAll($sqlse$arrval);
  96.         return $ret;
  97.     }
  98.  
  99.     /**
  100.      * 直前に実行されたSQL文を取得する.
  101.      *
  102.      * @param boolean $disp trueの場合、画面出力を行う.
  103.      * @return string SQL文
  104.      */
  105.     function getLastQuery($disp true{
  106.         $sql $this->conn->conn->last_query;
  107.         if($disp{
  108.             print($sql.";<br />\n");
  109.         }
  110.         return $sql;
  111.     }
  112.  
  113.     function commit({
  114.         $this->conn->query("COMMIT");
  115.     }
  116.  
  117.     function begin({
  118.         $this->conn->query("BEGIN");
  119.     }
  120.  
  121.     function rollback({
  122.         $this->conn->query("ROLLBACK");
  123.     }
  124.  
  125.     function exec($str$arrval array()) {
  126.         $this->conn->query($str$arrval);
  127.     }
  128.  
  129.     function autoselect($col$table$arrwhere array()$arrcon array()) {
  130.         $strw "";
  131.         $find false;
  132.         foreach ($arrwhere as $key => $val{
  133.             if(strlen($val0{
  134.                 if(strlen($strw<= 0{
  135.                     $strw .= $key ." LIKE ?";
  136.                 else if(strlen($arrcon[$key]0{
  137.                     $strw .= " "$arrcon[$key]" " $key ." LIKE ?";
  138.                 else {
  139.                     $strw .= " AND " $key ." LIKE ?";
  140.                 }
  141.  
  142.                 $arrval[$val;
  143.             }
  144.         }
  145.  
  146.         if(strlen($strw0{
  147.             $sqlse "SELECT $col FROM $table WHERE $strw ".$this->option;
  148.         else {
  149.             $sqlse "SELECT $col FROM $table ".$this->option;
  150.         }
  151.         $ret $this->conn->getAll($sqlse$arrval);
  152.         return $ret;
  153.     }
  154.  
  155.     function getAll($sql$arrval array()) {
  156.         $ret $this->conn->getAll($sql$arrval);
  157.         return $ret;
  158.     }
  159.  
  160.     function getSql($col$table$where{
  161.         if($where != ""{
  162.             // 引数の$whereを優先して実行する。
  163.             $sqlse "SELECT $col FROM $table WHERE $where $this->groupby . " " $this->order . " " $this->option;
  164.         else {
  165.             if($this->where != ""{
  166.                     $sqlse "SELECT $col FROM $table WHERE $this->where $this->groupby . " " $this->order . " " $this->option;
  167.                 else {
  168.                     $sqlse "SELECT $col FROM $table $this->groupby . " " $this->order . " " $this->option;
  169.             }
  170.         }
  171.         return $sqlse;
  172.     }
  173.  
  174.     function setOption($str{
  175.         $this->option = $str;
  176.     }
  177.  
  178.     function setLimitOffset($limit$offset 0$return false{
  179.         if (is_numeric($limit&& is_numeric($offset)){
  180.  
  181.             $option " LIMIT " $limit;
  182.             $option.= " OFFSET " $offset;
  183.  
  184.             if($return){
  185.                 return $option;
  186.             }else{
  187.                 $this->option.= $option;
  188.             }
  189.         }
  190.     }
  191.  
  192.     function setGroupBy($str{
  193.         $this->groupby = "GROUP BY " $str;
  194.     }
  195.  
  196.     function andwhere($str{
  197.         if($this->where != ""{
  198.             $this->where .= " AND " $str;
  199.         else {
  200.             $this->where = $str;
  201.         }
  202.     }
  203.  
  204.     function orWhere($str{
  205.         if($this->where != ""{
  206.             $this->where .= " OR " $str;
  207.         else {
  208.             $this->where = $str;
  209.         }
  210.     }
  211.  
  212.     function setWhere($str{
  213.         $this->where = $str;
  214.     }
  215.  
  216.     function setOrder($str{
  217.         $this->order = "ORDER BY " $str;
  218.     }
  219.  
  220.  
  221.     function setLimit($limit){
  222.         if is_numeric($limit)){
  223.             $this->option = " LIMIT " .$limit;
  224.         }
  225.     }
  226.  
  227.     function setOffset($offset{
  228.         if is_numeric($offset)){
  229.             $this->offset " OFFSET " .$offset;
  230.         }
  231.     }
  232.  
  233.     /**
  234.      * INSERT文を実行する.
  235.      *
  236.      * @param string $table テーブル名
  237.      * @param array $sqlval array('カラム名' => '値',...)の連想配列
  238.      * @return 
  239.      */
  240.     function insert($table$sqlval{
  241.         $strcol '';
  242.         $strval '';
  243.         $find false;
  244.  
  245.         if(count($sqlval<= return false;
  246.  
  247.         foreach ($sqlval as $key => $val{
  248.             $strcol .= $key ',';
  249.             if(eregi("^Now\(\)$"$val)) {
  250.                 $strval .= 'Now(),';
  251.             // 先頭に~があるとプレースホルダーしない。
  252.             else {
  253.                 $strval .= '?,';
  254.                 if($val != ""){
  255.                     $arrval[$val;
  256.                 else {
  257.                     $arrval[NULL;
  258.                 }
  259.             }
  260.             $find true;
  261.         }
  262.         if(!$find{
  263.             return false;
  264.         }
  265.         // 文末の","を削除
  266.         $strcol ereg_replace(",$","",$strcol);
  267.         // 文末の","を削除
  268.         $strval ereg_replace(",$","",$strval);
  269.         $sqlin "INSERT INTO $table($strcol") VALUES (" $strval ")";
  270.         // INSERT文の実行
  271.         $ret $this->conn->query($sqlin$arrval);
  272.  
  273.         return $ret;
  274.     }
  275.  
  276.     // INSERT文の生成・実行
  277.     // $table   :テーブル名
  278.     // $sqlval  :列名 => 値の格納されたハッシュ配列
  279.     function fast_insert($table$sqlval{
  280.         $strcol '';
  281.         $strval '';
  282.         $find false;
  283.  
  284.         foreach ($sqlval as $key => $val{
  285.                 $strcol .= $key ',';
  286.                 if($val != ""){
  287.                     $eval pg_escape_string($val);
  288.                     $strval .= "'$eval',";
  289.                 else {
  290.                     $strval .= "NULL,";
  291.                 }
  292.                 $find true;
  293.         }
  294.         if(!$find{
  295.             return false;
  296.         }
  297.         // 文末の","を削除
  298.         $strcol ereg_replace(",$","",$strcol);
  299.         // 文末の","を削除
  300.         $strval ereg_replace(",$","",$strval);
  301.         $sqlin "INSERT INTO $table($strcol") VALUES (" $strval ")";
  302.  
  303.         // INSERT文の実行
  304.         $ret $this->conn->query($sqlin);
  305.  
  306.         return $ret;
  307.     }
  308.  
  309.     /**
  310.      * UPDATE文を実行する.
  311.      *
  312.      * @param string $table テーブル名
  313.      * @param array $sqlval array('カラム名' => '値',...)の連想配列
  314.      * @param string $where WHERE句
  315.      * @param array $arradd $addcol用のプレースホルダ配列
  316.      * @param string $addcol 追加カラム
  317.      * @return 
  318.      */
  319.     function update($table$sqlval$where ""$arradd ""$addcol ""{
  320.         $strcol '';
  321.         $strval '';
  322.         $find false;
  323.         foreach ($sqlval as $key => $val{
  324.             if(eregi("^Now\(\)$"$val)) {
  325.                 $strcol .= $key '= Now(),';
  326.             // 先頭に~があるとプレースホルダーしない。
  327.             else {
  328.                 $strcol .= $key '= ?,';
  329.                 if($val != ""){
  330.                     $arrval[$val;
  331.                 else {
  332.                     $arrval[NULL;
  333.                 }
  334.             }
  335.             $find true;
  336.         }
  337.         if(!$find{
  338.             return false;
  339.         }
  340.  
  341.         if($addcol != ""{
  342.             foreach($addcol as $key => $val{
  343.                 $strcol .= "$key = $val,";
  344.             }
  345.         }
  346.  
  347.         // 文末の","を削除
  348.         $strcol ereg_replace(",$","",$strcol);
  349.         // 文末の","を削除
  350.         $strval ereg_replace(",$","",$strval);
  351.  
  352.         if($where != ""{
  353.             $sqlup "UPDATE $table SET $strcol WHERE $where";
  354.         else {
  355.             $sqlup "UPDATE $table SET $strcol";
  356.         }
  357.  
  358.         if(is_array($arradd)) {
  359.             // プレースホルダー用に配列を追加
  360.             foreach($arradd as $val{
  361.                 $arrval[$val;
  362.             }
  363.         }
  364.  
  365.         // INSERT文の実行
  366.         $ret $this->conn->query($sqlup$arrval);
  367.         return $ret;
  368.     }
  369.  
  370.     // MAX文の実行
  371.     function max($table$col$where ""$arrval array()) {
  372.         if(strlen($where<= 0{
  373.             $sqlse "SELECT MAX($col) FROM $table";
  374.         else {
  375.             $sqlse "SELECT MAX($col) FROM $table WHERE $where";
  376.         }
  377.         // MAX文の実行
  378.         $ret $this->conn->getOne($sqlse$arrval);
  379.         return $ret;
  380.     }
  381.  
  382.     // MIN文の実行
  383.     function min($table$col$where ""$arrval array()) {
  384.         if(strlen($where<= 0{
  385.             $sqlse "SELECT MIN($col) FROM $table";
  386.         else {
  387.             $sqlse "SELECT MIN($col) FROM $table WHERE $where";
  388.         }
  389.         // MIN文の実行
  390.         $ret $this->conn->getOne($sqlse$arrval);
  391.         return $ret;
  392.     }
  393.  
  394.     // 特定のカラムの値を取得
  395.     function get($table$col$where ""$arrval array()) {
  396.         if(strlen($where<= 0{
  397.             $sqlse "SELECT $col FROM $table";
  398.         else {
  399.             $sqlse "SELECT $col FROM $table WHERE $where";
  400.         }
  401.         // SQL文の実行
  402.         $ret $this->conn->getOne($sqlse$arrval);
  403.         return $ret;
  404.     }
  405.  
  406.     function getOne($sql$arrval array()) {
  407.         // SQL文の実行
  408.         $ret $this->conn->getOne($sql$arrval);
  409.         return $ret;
  410.  
  411.     }
  412.  
  413.     // 一行を取得
  414.     function getRow($table$col$where ""$arrval array()) {
  415.         if(strlen($where<= 0{
  416.             $sqlse "SELECT $col FROM $table";
  417.         else {
  418.             $sqlse "SELECT $col FROM $table WHERE $where";
  419.         }
  420.         // SQL文の実行
  421.         $ret $this->conn->getRow($sqlse$arrval);
  422.  
  423.         return $ret;
  424.     }
  425.  
  426.     // 1列取得
  427.     function getCol($table$col$where ""$arrval array()) {
  428.         if (strlen($where<= 0{
  429.             $sqlse "SELECT $col FROM $table";
  430.         else {
  431.             $sqlse "SELECT $col FROM $table WHERE $where";
  432.         }
  433.         // SQL文の実行
  434.         return $this->conn->getCol($sqlse0$arrval);
  435.     }
  436.  
  437.     /**
  438.      * レコードの削除
  439.      *
  440.      * @param string $table テーブル名
  441.      * @param string $where WHERE句
  442.      * @param array $arrval プレースホルダ
  443.      * @return 
  444.      */
  445.     function delete($table$where ""$arrval array()) {
  446.         if(strlen($where<= 0{
  447.             $sqlde "DELETE FROM $table";
  448.         else {
  449.             $sqlde "DELETE FROM $table WHERE $where";
  450.         }
  451.         $ret $this->conn->query($sqlde$arrval);
  452.         return $ret;
  453.     }
  454.  
  455.     function nextval($table$colname{
  456.         $sql "";
  457.         // postgresqlとmysqlとで処理を分ける
  458.         if (DB_TYPE == "pgsql"{
  459.             $seqtable $table "_" $colname "_seq";
  460.             $sql "SELECT NEXTVAL('$seqtable')";
  461.         }else if (DB_TYPE == "mysql"{
  462.             $sql "SELECT last_insert_id();";
  463.         }
  464.         $ret $this->conn->getOne($sql);
  465.  
  466.         return $ret;
  467.     }
  468.  
  469.     function currval($table$colname{
  470.         $sql "";
  471.         if (DB_TYPE == "pgsql"{
  472.             $seqtable $table "_" $colname "_seq";
  473.             $sql "SELECT CURRVAL('$seqtable')";
  474.         }else if (DB_TYPE == "mysql"{
  475.             $sql "SELECT last_insert_id();";
  476.         }
  477.         $ret $this->conn->getOne($sql);
  478.  
  479.         return $ret;
  480.     }
  481.  
  482.     function setval($table$colname$data{
  483.         $sql "";
  484.         if (DB_TYPE == "pgsql"{
  485.             $seqtable $table "_" $colname "_seq";
  486.             $sql "SELECT SETVAL('$seqtable', $data)";
  487.             $ret $this->conn->getOne($sql);
  488.         }else if (DB_TYPE == "mysql"{
  489.             $sql "ALTER TABLE $table AUTO_INCREMENT=$data";
  490.             $ret $this->conn->query($sql);
  491.         }
  492.  
  493.         return $ret;
  494.     }
  495.  
  496.     function query($n ,$arr ""$ignore_err false){
  497.         $result $this->conn->query($n$arr$ignore_err);
  498.         return $result;
  499.     }
  500.  
  501.     /**
  502.      * auto_incrementを取得する.
  503.      *
  504.      * @param string $table_name テーブル名
  505.      * @return integer 
  506.      */
  507.     function get_auto_increment($table_name){
  508.         // ロックする
  509.         $this->query("LOCK TABLES $table_name WRITE");
  510.  
  511.         // 次のIncrementを取得
  512.         $arrRet $this->getAll("SHOW TABLE STATUS LIKE ?"array($table_name));
  513.         $auto_inc_no $arrRet[0]["Auto_increment"];
  514.  
  515.         // 値をカウントアップしておく
  516.         $this->conn->query("ALTER TABLE $table_name AUTO_INCREMENT=?$auto_inc_no 1);
  517.  
  518.         // 解除する
  519.         $this->query('UNLOCK TABLES');
  520.  
  521.         return $auto_inc_no;
  522.     }
  523. }
  524.  
  525. ?>

Documentation generated on Fri, 24 Feb 2012 14:00:20 +0900 by Seasoft